<?php

	function tHeadString($fromTime, $toTime, $courtA, $isEnglish)
	{
		$tHeadStr = "<thead><tr><th>" . ( $isEnglish ? "Court" : "Kurt" ) . "</th>";
		for ($i = $fromTime; $i <= $toTime; $i ++)
		{
			$thStr = $i < 10 ? sprintf( "&nbsp;%d", $i ) : $i;
			$thStr .= $courtA ? ":00" : ":30";
			$tHeadStr .= "<th>" . $thStr . "</th>";
		}
		
		 $tHeadStr .= "</tr></thead>";
		 return $tHeadStr;
	}
	
	function trString($fromTime, $toTime, $courtReservationArray, $customerId, $fromIndex, $toIndex)
	{
		$trStr = "";
		for ($i = $fromIndex; $i < $toIndex; $i ++)
		{
			$trStr .= "<tr><td>$i</td>";
			$courtReservation = $courtReservationArray[$i];
			for ($j = $fromTime; $j <= $toTime; $j ++)
			{
				$reservationItem = $courtReservation->reserved[$j];
				
				$id = sprintf("\"td%d_%d\"", $i, $j );
				$tdTextClass = "";
				$tdText = "&nbsp";
				if ($reservationItem->customerId > -1 && $reservationItem->customerId == $customerId)
				{
					$tdText = "M";
					$tdTextClass = "reservationItemText";
				}

				$tdStr = "";
				switch ($reservationItem->reservationType)
				{
				case ReservationType::Free:
					$tdStr = ($customerId > -1) ? "<td id = $id class = \"reservationItemStyle\"></td>" : "<td>$tdText</td>";
					break;

				case ReservationType::Reserved:
					$tdStr = "<td id = $id class = \"reservationItemStyleReservation $tdTextClass\">$tdText</td>";
					break;

				case ReservationType::FixedTerm:
					$tdStr = "<td id = $id class = \"reservationItemStyleFixedTerm $tdTextClass\">$tdText</td>";
					break;
				case ReservationType::Holiday:
				case ReservationType::Failure:
					$tdStr = "<td class = \"reservationItemStyleFailure $tdTextClass\">$tdText</td>";
					break;
				case ReservationType::Tournament:
					$tdStr = "<td class = \"reservationItemStyleTournament $tdTextClass\">$tdText</td>";
					break;

				default:
					break;
				}
				
				$trStr .= $tdStr;
			}
			$trStr .= "</tr>";
		}
		
		return $trStr;
	}
	
	$courtATableStr = "<table class=\"reservationtable\">";
	$courtBTableStr = $courtATableStr;
	$courtATableStr .= tHeadString($fromTime, $toTime, true, $isEnglish);
	$courtBTableStr .= tHeadString($fromTime, $toTime, false, $isEnglish);
	$courtATableStr .= trString($fromTime, $toTime, $courtReservationArray, $customerId, 0, 3);
	$courtBTableStr .= trString($fromTime, $toTime, $courtReservationArray, $customerId, 3, 5);
	$courtATableStr .= "</table>";
	$courtBTableStr .= "</table>";
	
	echo $courtATableStr;
	echo $courtBTableStr;
?>
